//
//  SJAvatarBrowser.m
//  Headset
//
//  Created by KevinQQ on 15/6/13.
//  Copyright (c) 2015年 xxx.Headset. All rights reserved.
//

#import "SJAvatarBrowser.h"


static CGRect oldframe;
static ScaleImageViewGood *imageView;




@implementation SJAvatarBrowser

+(void)showImage:(UIImageView *)avatarImageView{
    
    UIImage *image=avatarImageView.image;
    
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    
    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:window];
   
    backgroundView.backgroundColor=[UIColor blackColor];
    backgroundView.alpha=1;
    
    imageView = [[ScaleImageViewGood alloc]initWithFrame:oldframe];
    
    
    imageView.image=image;
    imageView.tag=1;
    [backgroundView addSubview:imageView];
    
    [window addSubview:backgroundView];
    
    //向里或向外捏UIPinchGestureRecognizer (用于缩放)
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchVie:)];
    [backgroundView addGestureRecognizer:pinchGestureRecognizer];
    
  
//    // 双击放大
//    UITapGestureRecognizer *scaleBigTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
//    scaleBigTap.numberOfTapsRequired = 2;
//    scaleBigTap.numberOfTouchesRequired = 1;
//    [backgroundView addGestureRecognizer:scaleBigTap];

    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer: tap];

    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=CGRectMake(-1,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
        backgroundView.alpha=1;
        
    } completion:^(BOOL finished) {
        
    }];
}


//向里或向外捏UIPinchGestureRecognizer (用于缩放)
+ (void)pinchVie:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    
     UIView *view = imageView;

    static CGFloat la;
    if([pinchGestureRecognizer state] == UIGestureRecognizerStateBegan) {
       
        la = [pinchGestureRecognizer scale];
    }
    
    CGFloat newScale = 1 -  (la - [pinchGestureRecognizer scale]);
    
    if ([pinchGestureRecognizer state] == UIGestureRecognizerStateBegan ||
        [pinchGestureRecognizer state] == UIGestureRecognizerStateChanged) {
        
        CGFloat currentScale = [[view.layer valueForKeyPath:@"transform.scale"] floatValue];
        
        // Constants to adjust the max/min values of zoom
        const CGFloat kMaxScale = 10.0;
        const CGFloat kMinScale = 0.5;
        
        newScale = MIN(newScale, kMaxScale / currentScale);
        newScale = MAX(newScale, kMinScale / currentScale);
        
        view.transform = CGAffineTransformScale(view.transform, newScale, newScale);
        
        la = [pinchGestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call
      
    }

}


+(void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView=tap.view;
    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.4 animations:^{
        imageView.frame=oldframe;
        backgroundView.alpha=0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
}



@end
